package com.kmproject.haifu.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.di.component.DaggerMoreHousePublishComponent;
import com.kmproject.haifu.mvp.contract.MoreHousePublishContract;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.presenter.MoreHousePublishPresenter;
import com.kmproject.haifu.mvp.ui.adapter.FragmentTitlesAdapter;
import com.kmproject.haifu.mvp.ui.fragment.MoreHousePublishRentFragment;
import com.kmproject.haifu.mvp.ui.fragment.MoreHousePublishSecondHouseFragment;
import com.kmproject.haifu.mvp.ui.fragment.MyCollectHouseRentFragment;
import com.kmproject.haifu.mvp.ui.fragment.MyCollectSecondHandHouseFragment;
import com.kmproject.haifu.mvp.ui.widget.CircleImageView;
import com.kmproject.haifu.mvp.ui.widget.NoScrollViewPager;
import com.kmproject.haifu.mvp.ui.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/17/2019 21:17
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MoreHousePublishActivity extends BaseActivity<MoreHousePublishPresenter> implements MoreHousePublishContract.View {

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.MoreHousePublish_header_img)
    CircleImageView MoreHousePublishHeaderImg;
    @BindView(R.id.MoreHousePublish_show_tv1)
    TextView MoreHousePublishShowTv1;
    @BindView(R.id.MoreHousePublish_tv2)
    TextView MoreHousePublishTv2;
    @BindView(R.id.MoreHousePublish_tv3)
    TextView MoreHousePublishTv3;
    @BindView(R.id.MoreHousePublish_indicator)
    MagicIndicator MoreHousePublishIndicator;
    @BindView(R.id.MoreHousePublish_viewpager)
    NoScrollViewPager MoreHousePublishViewpager;
    List<Fragment> fragments = new ArrayList<>();
    List<String> titles = null;
    int selectIndex = 0;
    ImageLoader glideImage;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMoreHousePublishComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_more_house_publish; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        titles = Arrays.asList(getResources().getStringArray(R.array.collect_title));
        fragments.add(MoreHousePublishRentFragment.newInstance());
        fragments.add(MoreHousePublishSecondHouseFragment.newInstance());
        fragments.add(MoreHousePublishSecondHouseFragment.newInstance());
        fragments.add(MoreHousePublishSecondHouseFragment.newInstance());
        MoreHousePublishViewpager.setAdapter(new FragmentTitlesAdapter(getSupportFragmentManager(), this, titles, fragments));
        MoreHousePublishViewpager.setCurrentItem(0);
        CommonNavigator commonNavigator = new CommonNavigator(this);
//        final  int defaultWidth=getViewHeight(MyCollectMagicIndicator,false);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(" " + titles.get(index) + " ");
//                simplePagerTitleView.setWidth((getWindow().getWindowManager().getDefaultDisplay().getWidth()-10)/4);
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_191919));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_191919));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(15, 0, 5, 0);//4个参数按顺序分别是左上右下
                simplePagerTitleView.setLayoutParams(layoutParams);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MoreHousePublishViewpager.setCurrentItem(index);
                    }
                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setLineHeight(11.0f);
                linePagerIndicator.setColors(ArmsUtils.getColor(MoreHousePublishActivity.this, R.color.color_F9D35A));

                return linePagerIndicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }

        });
        MoreHousePublishIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(MoreHousePublishIndicator, MoreHousePublishViewpager);
        MoreHousePublishViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                selectIndex = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        HouseRentListShowInfoBean    houseRentListShowInfoBean=(HouseRentListShowInfoBean)getIntent().getSerializableExtra("HouseRentListShowInfoBean");
        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(this).imageLoader();
        }
        glideImage.loadImage(this, ImageConfigImpl.builder().url(houseRentListShowInfoBean.getAvatar()).imageRadius(5)
                .imageView(MoreHousePublishHeaderImg)
                .build());

        MoreHousePublishShowTv1.setText(houseRentListShowInfoBean.getLandlord());
        MoreHousePublishTv2.setText(houseRentListShowInfoBean.getLandlordTypeName());
        MoreHousePublishTv3.setText("来海服" + (houseRentListShowInfoBean.getLogDiffDay() > 0 ? houseRentListShowInfoBean.getLogDiffDay() + "天" : (houseRentListShowInfoBean.getLogDiffHour() > 0 ? houseRentListShowInfoBean.getLogDiffHour() + "小时" : houseRentListShowInfoBean.getLogDiffMinute() + "分钟")) + ", " + houseRentListShowInfoBean.getLandlordGender()
       +" " +(houseRentListShowInfoBean.getUpdDiffDay() > 0 ? houseRentListShowInfoBean.getUpdDiffDay() + "天" : (houseRentListShowInfoBean.getUpdDiffHour() > 0 ? houseRentListShowInfoBean.getUpdDiffHour() + "小时" : houseRentListShowInfoBean.getUpdDiffMinute() + "分钟")) + "前来过");


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
